﻿function getFloat ( v )
{
	var j = parseFloat ( v );
	if ( isNaN ( j ) ) j = 0;
	return j;
}




/*
function Foo(){
    this.x = 1;
    this.bjr();
}

Foo.prototype.bjr = function(){
    ////alert("bjr");
}

 
var obj = new Foo ();
*/


/* NOOOOOOOOOOOOOOOOON C DLA MERDE
document.body.addEventListener('touchmove', function(e) {
  // This prevents native scrolling from happening.
  e.preventDefault();
}, false);
 */



function Scroller (element) {
//Scroller = function(element) {
  ////alert ("__bonjour inside");
  this.element = element;//this.element = this;
  this.startTouchY = 0;
  ////alert ("bonjour inside2");
  this.coucou ();
  this.animateTo(0);
  this.deltaY = 0;
  this.startTime = 0;
  this.stopTime = 0;
  this.mvtStopEnd = 0;
  element.addEventListener('touchstart', this, false);
  element.addEventListener('touchmove', this, false);
  element.addEventListener('touchend', this, false);
}

Scroller.prototype.coucou = function ()
{
  ////alert("coucou");
}























Scroller.prototype.animateTo = function(offsetY) {
  //alert("animateTo");
  this.contentOffsetY = offsetY;

  // We use webkit-transforms with translate3d because these animations
  // will be hardware accelerated, and therefore significantly faster
  // than changing the top value.
  this.element.style.webkitTransform = 'translate3d(0, ' + offsetY + 'px, 0)';
  //alert("animateTo 2");
}

// Implementation of this method is left as an exercise for the reader.
// You need to measure the current position of the scrollable content
// relative to the frame. If the content is outside of the boundaries
// then simply reposition it to be just within the appropriate boundary.
Scroller.prototype.snapToBounds = function() {
	//alert("snapToBounds");
  //...
  // TODO: plutot utiliser this.contentOffsetY et this.animateTo (newOffsetY)
  /*
  var t = getFloat(this.element.style.top);
  if ( t < 0 )
    this.element.style.top = 0;
    var h = getFloat(this.element.style.height);
    var v = t + h;
    var ph = getFloat(this.element.parentNode.style.height);
    var z = ph - h;
    if ( v > ph )
      this.element.style.top = z + 'px';
    */
  if ( this.contentOffsetY < 0 )
    this.contentOffsetY = 0;
  var h = getFloat ( this.element.style.height );
  var v = this.contentOffsetY + h;
  var ph = getFloat ( this.element.parentNode.style.height );
  var z = ph - h;
  if ( v > ph )
    this.animateTo ( z );
}

// Implementation of this method is left as an exercise for the reader.
// You need to consider whether their touch has moved past a certain
// threshold that should be considered ‘dragging’.
Scroller.prototype.isDragging = function() {
	//alert("isDragging");
  //...
  var v = this.deltaY;
  if ( v < 0 )
	v = -v;
  if ( v > 0.00000005 ) // TODO: choisir une bonne valeur
    return true;
  return false;
}















// Implementation of this method is left as an exercise for the reader.
// You need to consider the end velocity of the drag was past the
// threshold required to initiate momentum.
Scroller.prototype.shouldStartMomentum = function() {
	//alert("shouldStartMomentum");
  //...
  if ( this.getEndVelocity () > 0.00000005 ) // TODO: choisir une bonne valeur
    return true;
  return false;
}

// Implement getEndVelocity using the
// start and end position / time.
Scroller.prototype.getEndVelocity = function() {
	//alert("getEndVelocity");
  // ...
  // this.contentOffsetY
  // this.contentStartOffsetY
  // this.startTime
  // this.stopTime
  return ( this.contentOffsetY - this.contentStartOffsetY ) / ( this.stopTime - this.startTime ); // unité : px / ms
}

Scroller.prototype.doMomentum = function() {
	//alert("doMomentum");
  // Calculate the movement properties. Implement getEndVelocity using the
  // start and end position / time.
  var velocity = this.getEndVelocity();
  var acceleration = velocity < 0 ? -0.0005 : 0.0005;
  var displacement = - (velocity * velocity) / (2 * acceleration);
  var time = - velocity / acceleration;
  this.mvtStopEnd = getTimer () + time;
  // Set up the transition and execute the transform. Once you implement this
  // you will need to figure out an appropriate time to clear the transition
  // so that it doesn’t apply to subsequent scrolling.
  this.element.style.webkitTransition = '-webkit-transform ' + time +
      'ms cubic-bezier(0.33, 0.66, 0.66, 1)';
  setTimeout ( this.clearTransition, time );
  var newY = this.contentOffsetY + displacement;
  this.contentOffsetY = newY;
  this.element.style.webkitTransform = 'translate3d(0, ' + newY + 'px, 0)';
}










Scroller.prototype.clearTransition = function() {
	//alert("clearTransition");
	this.element.style.webkitTransition = '';
}

Scroller.prototype.isDecelerating = function() {
	//alert("isDecelerating");
	//...
	// pas sûr
	if ( getTimer () < this.mvtStopEnd )
		return true;
	return false;
}

Scroller.prototype.stopMomentum = function() {
	//alert("stopMomentum");
  if (this.isDecelerating()) {
    // Get the computed style object.
    var style = document.defaultView.getComputedStyle(this.element, null);
    // Computed the transform in a matrix object given the style.
    var transform = new WebKitCSSMatrix(style.webkitTransform);
    // Clear the active transition so it doesn’t apply to our next transform.
    this.element.style.webkitTransition = '';
    // Set the element transform to where it is right now.
    this.animateTo(transform.m42);
  }
}






Scroller.prototype.onTouchStart = function(e) {
	alert("onTouchStart");
  // This will be shown in part 4.
  this.stopMomentum();

  this.startTouchY = e.touches[0].clientY;
  this.startTime = getTime ();
  this.contentStartOffsetY = this.contentOffsetY;
}

Scroller.prototype.onTouchMove = function(e) {
	alert("onTouchMove");
  var currentY = e.touches[0].clientY;
  var deltaY = currentY - this.startTouchY;
  var newY = deltaY + this.contentStartOffsetY;
  this.deltaY = deltaY;
  if (this.isDragging()) {
    this.animateTo(newY);
  }
  /*if (this.isDragging()) {
    var currentY = e.touches[0].clientY;
    var deltaY = currentY - this.startTouchY;
    var newY = deltaY + this.contentStartOffsetY;
    this.animateTo(newY);
  }*/
}

Scroller.prototype.onTouchEnd = function(e) {
	alert("onTouchEnd");
  this.stopTime = getTime ();
  if (this.isDragging()) {
    if (this.shouldStartMomentum()) {
      // This will be shown in part 3.
      this.doMomentum();
    } else {
      this.snapToBounds();
    }
  }
}

Scroller.prototype.handleEvent = function(e) {
	//alert("handleEvent");
  switch (e.type) {
    case "touchstart":
      this.onTouchStart(e);
      break;
    case "touchmove":
      this.onTouchMove(e);
      break;
    case "touchend":
      this.onTouchEnd(e);
      break;
  }
}











function initScroll ()
{
	//alert ("bonjour");
	v = new Scroller ( document.getElementById ( "hasToScroll" ) );
	//alert (v);
	//alert ("bonjour2");
	
	document.body.addEventListener('touchmove', function(e) {
	  // This prevents native scrolling from happening.
	  e.preventDefault();
	}, false);
	
	//alert ("bonjour3");
}
